<?php
/**
 * 推荐关注Top10管理
 * Date: 2021/4/23
 * Author: sunlx
 */
namespace Frieza\Modules\RecommendFollow\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use Doraemon\model\Member as mMember;
use Doraemon\tools\Log;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Doraemon\service\UserGrowth\RecommendFollow;
use Doraemon\model\AdmRecommendFollow;

class IndexController extends Controller
{
    public function index(Request $request, Content $content)
    {
        $pageSize = $request->input("per_page", 20);
        $page = $request->input("page", 1);
        $params = $request->input();

        $search = [];
        if (isset($params['uid']) && $params['uid']) {
            $search['uid'] = $params['uid'];
        }
        if (isset($params['type']) && $params['type'] !== '') {
            $search['type'] = $params['type'];
        }
        $list = RecommendFollow::getRecommendUsersByPage($page, $pageSize, $search);

        $gridModel = new pGridDataModel($list['list'], $list['total'], [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->disableActions();

                $grid->filter(function($filter){
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    $filter->equal('uid');
                    $filter->equal('type', '身份')->radio([
                        '' => '全部',
                        mMember::USER_COMMON => '普通用户',
                        mMember::AUTH_USER_BRAND => '品牌用户',
                        mMember::AUTH_USER_DESIGNER => '设计师用户'
                    ]);
                });

                $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
                    $create->text('uid', 'UID');
                });

                $grid->column("id", 'ID');
                $grid->column("uid", '用户')->userBasicInfo();
                $grid->column("fans_num", '粉丝数');
                $grid->column("description", '描述')->editable();
                $grid->column("data_status","状态")->editable('select', AdmRecommendFollow::STATUS_DESC);
                $grid->column("edittime", '更新时间')->display(function () {
                    return date("Y-m-d H:i:s", $this->edittime);
                });
                $grid->column("admin_id", '操作人员')->display(function () {
                    return "<span style='width:80px;display: inline-block;'>".Admin::user()->find($this->admin_id)->name."</span>";
                });
            });

        $tab = new Tab();
        $tab->add('用户列表', $gridList->render(), true);
        $content->row($tab);
        return $content
            ->header('新用户推荐关注')
            ->description(' ')
            ->breadcrumb(['text' => '用户列表']);
    }

    public function add(Request $request)
    {
        try {
            $params = $request->all();
            unset($params['_token']);

            $result = RecommendFollow::addRecommendUser($params['uid']);

            $code = $result['status'] ? 200 : 400;
            return response()->json($result, $code);
        } catch (\Exception $e) {
            Log::debug($e->getMessage());
            return response()->json([
                'status' => false,
                'message' => "操作失败"
            ], 400);
        }
    }

    public function edit(Request $request, $id = 0)
    {
        try {
            $params = $request->all();
            unset($params['_token']);

            $data[$params['name']] = $params['value'];
            $result = RecommendFollow::editRecommendUser($id, $data);

            $code = $result['status'] ? 200 : 400;
            return response()->json($result, $code);
        } catch (\Exception $e) {
            Log::debug($e->getMessage());
            return response()->json([
                'status' => false,
                'message' => "操作失败"
            ], 400);
        }
    }
}
